<!--
/// 
/// MaDAson - Toolkit for Model Driven Software Development
///
/// Copyright [2006] Marcus Lankenau
/// 
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///  
///     http://www.apache.org/licenses/LICENSE-2.0
///  
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
/// 
-->
<%@ Page Language="C#" %>
<%@ Import Namespace="Madason.MetaModel" %>
<%@ Import Namespace="Madason.MetaModel.DAO" %>
package <%= Class.Namespace.FullName %>;

import java.util.*;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * <%= Class.Documentation.Replace("\n", "\n	* ") %>
 */
public class <%= Class.Name + "Access" + Context.DatabaseStrategy.GetClassSuffix() %> extends <%= Class.Name %>Access
{   

<% foreach (Procedure proc in Class.GetProcedures()) { %>


	/**
	 * <%= proc.Name %>
	 */
	public <%= proc.ReturnTypeName %> <%= proc.Name %>(Connection conn<%= proc.ParameterString == "" ? "" : ", " %><%= proc.ParameterString %>)
		throws SQLException
	{
		String query = "<%= proc.QueryString %>";

		java.sql.CallableStatement stmt = null;
		try
		{
			stmt = conn.prepareCall(query);		
	
<% if (proc.ReturnType != null &&  proc.ReturnType.Name != "void") { %>
			stmt.registerOutParameter(1, <%= JdbcInterface.GetSqlType(proc.ReturnType) %>);
<% } %>	
<% int idx = (proc.ReturnType != null &&  proc.ReturnType.Name != "void") ? 2 : 1; %>	
<% foreach (Parameter param in proc.Parameters) { %>	
<%   if (param.Dir == ParameterDir.In) { %>
			stmt.<%= JdbcInterface.GetPreparedStatementSetter(param.Type) %>(<%= idx %>, <%= param.Name %>);
<%   } else { %>
			stmt.registerOutParameter(<%= idx %>, <%= JdbcInterface.GetSqlType(param.Type) %>);
<%   } %>
<%   idx++; %>
<% } %>			
	
			stmt.execute();
			
<% if (proc.HasOutParameter) { %>			
			<%= proc.ReturnTypeName %> results = new <%= proc.ReturnTypeName %>();
<% idx = (proc.ReturnType != null &&  proc.ReturnType.Name != "void") ? 2 : 1; %>	
<% foreach (Parameter param in proc.Parameters) { %>	
<%   if (param.Dir == ParameterDir.Out) { %>
<%      if (param.Type.Name == "Date")
		{ %>
			results.<%= param.Name%> = stmt.<%= JdbcInterface.GetResultsetGetter(param.Type)%>(convertDate(<%= idx%>));
<%      }
		else
		{ %>
			results.<%= param.Name%> = stmt.<%= JdbcInterface.GetResultsetGetter(param.Type)%>(<%= idx%>);
<%      } %>
<%   } %>
<%   idx++; %>
<% } %>			
			return results;
<% } else if (proc.ReturnType != null && proc.ReturnType.Name != "void") { %>			
			return stmt.<%= JdbcInterface.GetResultsetGetter(proc.ReturnType)%>(1);
<% } %>	
		
		}
		finally
		{
			if (stmt != null)
				stmt.close();
		}			
	}
<% } %>
}

<script runat="server">
	private ProcedureContainer @class;

	
	public ProcedureContainer Class
	{
		get { return @class; }
		set { @class = value; }
	}

	protected string FinderParamSep(Finder finder)	
	{
		return finder.Parameters.Count > 0 ? ", " : "";
	}
</script>
